﻿using JNTApp.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace JNTApp.util
{
    class HttpHelper
    {
        public static void Init() {

        }


        public static string POST(string uri,string data)
        {
            if (data == null)
            {
                data = "";
            }

            HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
            request.Method = "POST";                        //请求方法
            request.ProtocolVersion = new Version(1, 1);    //Http/1.1版本
            request.UserAgent = "JNET/1.0";
            request.Headers.Add("x-requested-with", "XMLHttpRequest");
            request.ContentType = "application/x-www-form-urlencoded";
            if (Variable.JSESSION != null)
            {
                request.Headers.Add("Cookie", "JSESSIONID=" + Variable.JSESSION);
            }
            byte[] bs = Encoding.ASCII.GetBytes(data);
            request.ContentLength = bs.Length;

            using (Stream writer = request.GetRequestStream())
            {
                writer.Write(bs, 0, bs.Length);
            }

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            string result = null;
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")))
            {
                result = reader.ReadToEnd();
            }

            return result;
        }

        public static string GET(string uri)
        {
            HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
            request.Method = "GET";                        //请求方法
            request.UserAgent = "JNET/1.0";
            request.Headers.Add("x-requested-with", "XMLHttpRequest");
            request.ProtocolVersion = new Version(1, 1);   //Http/1.1版本
            if (Variable.JSESSION != null)
            {
                request.Headers.Add("Cookie", "JSESSIONID="+Variable.JSESSION);
            }
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            string result = null;
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")))
            {
                result = reader.ReadToEnd();
            }

            return result;
        }

        public static string GET(string uri,Dictionary<string,string> param)
        {
            if (param != null)
            {
                foreach (var key in param.Keys)
                {
                    if (uri.IndexOf('?') >= 0)
                    {
                        uri += "&" + key + "=" + param[key];
                    }
                    else
                    {
                        uri += "?" + key + "=" + param[key];
                    }
                }
            }
            return GET(uri);
        }
    }
}
